package cn.itcast.rocketmq.spring.ordermsg;

import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

/**
 * Created by bothin
 * At 2020/3/23 17:27
 * Description:
 */
@Component
public class OrderProducer {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 相关类：
     *
     *public class RocketMQTemplate extends AbstractMessageSendingTemplate<String> implements InitializingBean, DisposableBean {
     *
     *      **************************
     *      同步顺序发送
     *      public SendResult syncSendOrderly(String destination, Message<?> message, String hashKey)
     *      public SendResult syncSendOrderly(String destination, Message<?> message, String hashKey, long timeout)
     *      public SendResult syncSendOrderly(String destination, Object payload, String hashKey)
     *      public SendResult syncSendOrderly(String destination, Object payload, String hashKey, long timeout)
     *
     *      **************************
     *      异步顺序发送
     *      public void asyncSendOrderly(String destination, Message<?> message, String hashKey, SendCallback sendCallback, long timeout)
     *      public void asyncSendOrderly(String destination, Message<?> message, String hashKey, SendCallback sendCallback)
     *      public void asyncSendOrderly(String destination, Object payload, String hashKey, SendCallback sendCallback)
     *      public void asyncSendOrderly(String destination, Object payload, String hashKey, SendCallback sendCallback, long timeout)
     *
     *      **************************
     *      单向顺序发送
     *      public void sendOneWayOrderly(String destination, Message<?> message, String hashKey)
     *      public void sendOneWayOrderly(String destination, Object payload, String hashKey)
     */

    public void sendInOrder(String topic, String msg,String hashKey){
        rocketMQTemplate.setMessageQueueSelector((list, message, o) -> list.get(Integer.parseInt(o.toString())));
        for(int i=0;i<10;i++){
            SendResult result=rocketMQTemplate.syncSendOrderly(topic,msg,hashKey);
        }
    }
}
